﻿using System;
using System.Web.UI;
using JQuery.WebKit.Base;
using JQuery.WebKit.Util;
using JQuery.WebKit.WebCommon;
using JQuery.WebKit.WebCommon.Attributes;

#region resources
[assembly: WebResource("JQuery.WebKit.JUpload.jquery.uploadify.v2.1.4.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JUpload.jquery.uploadify.v2.1.4.min.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JUpload.swfobject.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JUpload.swfobject.min.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JUpload.uploadify.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("JQuery.WebKit.JUpload.cancel.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.JUpload.uploadify.swf", "application/x-shockwave-flash")]
#endregion
namespace JQuery.WebKit
{
    public class JUpload : WebControlBase
    {
        [JavaScript("uploader", Category = "options")]
        public string Uploader { get; set; }

        [JavaScript("script", Category = "options")]
        public string Handler { get; set; }

        [JavaScript("cancelImg", Category = "options")]
        public string CancelImg { get; set; }

        [JavaScript("fileExt", Category = "options")]
        public string Extensions { get; set; }

        [JavaScript("fileDesc", Category = "options")]
        public string Description { get; set; }

        [JavaScript("folder", Category = "options", DefaultValue = "/uploads")]
        public string UploadFolder { get; set; }

        [JavaScript("buttonText", Category = "options")]
        public string ButtonText { get; set; }

        [JavaScript("auto", Category = "options", DefaultValue = true)]
        public bool? AutoUpload { get; set; }

        [JavaScript("multi", Category = "options", DefaultValue = false)]
        public bool? MultiUpload { get; set; }

        [JavaScript("sizeLimit", Category = "options")]
        public int SizeLimit { get; set; }

        [JavaScript("scriptData", Category = "options")]
        public string ScriptData { get; set; }

        [JavaScript("onSelectOnce", Category = "options")]
        public string OnSelectOnce { get; set; }

        [JavaScript("width", Category = "options")]
        public int ButtonWidth { get; set; }

        [JavaScript("onComplete", Category = "options")]
        public string OnComplete { get; set; }

        public JUpload()
            : base(HtmlTextWriterTag.Div)
        { }

        public virtual void SetDefaultValues()
        {
            if (string.IsNullOrEmpty(Uploader))
            {
                Uploader = Page.ClientScript.GetWebResourceUrl(typeof(JUpload), "JQuery.WebKit.JUpload.uploadify.swf");
            }
            if (string.IsNullOrEmpty(CancelImg))
            {
                CancelImg = Page.ClientScript.GetWebResourceUrl(typeof(JUpload), "JQuery.WebKit.JUpload.cancel.png");
            }

            if (string.IsNullOrEmpty(Handler))
            {
                throw new ArgumentNullException("Server Side Handler cannot be null!");
            }

            if (SizeLimit == 0)
            {
                SizeLimit = JUtil.GetMaxRequestLength() * 1024;
            }

            if (!string.IsNullOrEmpty(Extensions) && string.IsNullOrEmpty(Description))
            {
                throw new ArgumentNullException("Cannot set extension without file description!");
            }

            if (!string.IsNullOrEmpty(ScriptData) && !string.IsNullOrEmpty(UploadFolder))
            {
                if (ScriptData[0] == '}')
                {
                    ScriptData = ScriptData.Substring(1, ScriptData.Length - 1);
                }

                if (ScriptData[ScriptData.Length - 1] == '}')
                {
                    ScriptData = ScriptData.Substring(0, ScriptData.Length - 1);
                }
                ScriptData = string.Format("{{ {0}, folder: '{1}' }}", ScriptData, UploadFolder);
            }

            if (!string.IsNullOrEmpty(OnComplete))
            {
                OnComplete = string.Format("function(event, ID, fileObj, response, data) {{ {0} }}", OnComplete);
            }

            if (!string.IsNullOrEmpty(OnSelectOnce))
            {
                OnSelectOnce = string.Format("function(event, data) {{ {0} }}", OnSelectOnce);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            SetDefaultValues();

            base.RegisterJQueryResource("JQuery.WebKit.JUpload.swfobject.js");
            base.RegisterJQueryResource("JQuery.WebKit.JUpload.jquery.uploadify.v2.1.4.js");
            base.RegisterCSSResource("JQuery.WebKit.JUpload.uploadify.css");

            //string script = string.Format("$(document).ready(function() {{ var {0} = new qq.FileUploader({{ element: {1}, action: '{2}', allowedExtensions: [{3}], }}); }});", this.ID, this.ClientID, this.Handler, Extensions);
            //string script = string.Format("var {0} = new qq.FileUploader({{ element: $('#{1}')[0], action: '{2}', allowedExtensions: [{3}], }});", this.ID, this.ClientID, this.Handler, Extensions);

            //string script = string.Format("$('#{0}').uploadify({{ 'uploader': '{1}', 'script': '{2}', 'cancelImg': '{3}', 'folder' : '/uploads', 'auto': true }});",
            //    this.ClientID,
            //    Page.ClientScript.GetWebResourceUrl(typeof(JUpload), "JQuery.WebKit.JUpload.uploadify.swf"),
            //    Handler,
            //    Page.ClientScript.GetWebResourceUrl(typeof(JUpload), "JQuery.WebKit.JUpload.cancel.png"));

            string script = string.Format("$('#{0}').uploadify({1});", this.ClientID, this.GetJSProperties("options"));
            base.AddBodyScript(script);
            //this.Page.Resources.Add(new WebScriptResource(typeof(JUpload), ResourceType.Javascript, script, true));

            base.OnPreRender(e);
        }
    }
}
